<template>
	<view class="box">
		<!-- #ifdef MP-WEIXIN -->
		<view class="head" :style="'padding-top:'+statusBarHeight1+'px;padding-right:'+ (WXwidth + 15) +'px;height:' + (statusBarHeight1 + 44) + 'px'">
		<!-- #endif -->
		<!-- #ifndef MP-WEIXIN -->
		<view class="head" :style="'padding-top:'+(statusBarHeight1+10)+'px;height:' + (statusBarHeight1 + 54) + 'px'">
		<!-- #endif -->
			<image class="fanhui" @click="fanhui" src="https://pic.bangbangtongcheng.com/static/fanhui_bai.png"
					mode=""></image>
			<view class="centers">预览效果</view>
			<view class="fanhui"></view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<view
			:style="'padding-top:'+statusBarHeight1+'px;height:' + (statusBarHeight1 + 44) + 'px;background-color: #64B6A8;'">
		</view>
		<!-- #endif -->
		<!-- #ifndef MP-WEIXIN -->
		<view
			:style="'padding-top:'+statusBarHeight1+'px;height:' + (statusBarHeight1 + 54) + 'px;background-color: #64B6A8;'">
		</view>
		<!-- #endif -->
		<view class="center">
			<view class="title">
				{{title}}
			</view>
			<view style="width: 750rpx;display: flex;justify-content: center;" @click="ViewImage($commonResume + data.picture)">
				<image class="image" :src="$commonResume + data.picture" mode=""></image>
			</view>
		</view>
		<view class="footer">
			<view class="btn" @click="obtainResumeTemplates(1)">
				使用该模板
			</view>
			<view class="btn" @click="obtainResumeTemplates(2)">
				保存到本地
			</view>
		</view>
		<uniBall ref="uniBall"></uniBall>
	</view>
</template>
<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	//#ifdef H5
	import wxshare from "@/utils/index.js";
	//#endif
	import {
		mapGetters
	} from "vuex";
	export default {
		data() {
			return {
				userInfo: uni.getStorageSync("Pduser"),
				WXwidth: '',
				index:0,
				curriculumVitae:{},
				data:{},
				title:'',
			};
		},
		onLoad(options) {
			var than = this;
			// 获取微信小程序胶囊和据右边宽度
			// #ifdef MP-WEIXIN
			// 获取胶囊信息
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			let getWindowInfo = uni.getWindowInfo().screenWidth
			this.WXwidth = getWindowInfo - (menuButtonInfo.right - menuButtonInfo.width)
			// #endif
			const JLMB1_key = JSON.parse(JSON.stringify(uni.getStorageSync('JLMB1_key')));
			if (JLMB1_key) {
				console.log(uni.getStorageSync('JLMB1_key'),'8-8-8-')
				this.curriculumVitae = uni.getStorageSync('JLMB1_key') || {};
				// uni.removeStorageSync('JLMB1_key');
			}
			uni.showLoading({
			    title: '加载中',
			    mask: false
			})
			this.obtainResumeTemplate(options.attachmentConversion)
			this.title = options.title
			/* #ifdef H5 */
			this.jinzhi();
			/* #endif */
		},
		computed: {
			...mapGetters(["userid", "islogin", "statusBarHeight1", "userinfo"])
		},
		methods: {
			ViewImage(e) {
				let headerImage = []
				headerImage.push(e)
				uni.previewImage({
					urls:headerImage,
					current: e,
				});
			},
			swiperC1(e) {
				this.index = e.detail.current;
			},
			obtainResumeTemplate(attachmentConversion){
				console.log(attachmentConversion)
				this.curriculumVitae.resumeTemplatePath = attachmentConversion;
				delete this.curriculumVitae.userInformation;
				console.log(this.curriculumVitae)
				this.curriculumVitae.workExperience = JSON.parse(this.curriculumVitae.workExperience) 
				this.$myRequest.post("/api/mobile/personnelOwner/resumePreview", this.curriculumVitae,).then((res) => {
					uni.hideLoading();
					this.data = res
				});
			},
			fanhui(){
				uni.navigateBack({  
				    delta: 1
				});
			},
			obtainResumeTemplates(indexs){
				if(indexs == 1){
					let datas = {}
					datas.enclosureWord = this.data.attachmentAddress
					datas.enclosurePdf = this.data.pdf
					datas.pictureAttachment = this.data.picture
					setTimeout(()=>{
						let pages = getCurrentPages();
						let prevPage = pages[ pages.length - 3 ];
						prevPage.$vm.resumeTemplate = datas
						uni.navigateBack({
						    delta: 2
						});
					},500)
				}else{
					this.save(this.$commonResume + this.data.pdf)
				}
			},
			save(url){
				// #ifdef APP-PLUS
				if(uni.getSystemInfoSync().platform == 'android'){
					this.appSave(url);
				}else{
					this.iosSave(url);
				}
				// #endif
				// #ifdef MP-WEIXIN
				this.WXSave(url);
				// #endif
				// #ifdef H5
				window.location.href = url
				// #endif
			},
			//app保存文件
			iosSave(url){
				uni.downloadFile({
				    url: url,
				    success: function(res) {
				    var filePath = res.tempFilePath; 
				    
				        //如果是文件走这里
						uni.saveFile({
									tempFilePath: res.tempFilePath,//文件的临时路径
									success: function(res) {
										const savedFilePath = res.savedFilePath;
										// 打开文件
										uni.openDocument({
											filePath: savedFilePath,
											success: function(res) {
												uni.hideLoading()
											},
											fail: function(res) {},
											complete: function(res) {
												setTimeout(uni.hideLoading(), 4000)
											},
										});
									},
									fail: function(err) {	}
								});
					}
				});
			},
			appSave(url){
				uni.showLoading({
				title: '正在下载'
				});
				// 本地路径开头使用file://，跟上手机文件本地目录storage/emulated/0，
				// 这时用户文件管理器能看到的了，之后创建 连信 作为文件夹，
				// 后缀是用于文件命名和格式修改，大家可以使用变量。
				let dtask = plus.downloader.createDownload(url, {
					filename: `file://storage/emulated/0/bangbangtongcheng/${this.title}` //利用保存路径，实现下载文件的重命名
				},(d, status)=> {
					console.log(d,status);
					//d为下载的文件对象
					if (status == 200) {
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '已保存到文件夹：bangbangtongcheng/' + this.title, //保存路径
							duration: 5000,
						});
						//下载成功,d.filename是文件在保存在本地的相对路径，使用下面的API可转为平台绝对路径
						let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
						console.log(fileSaveUrl);
						setTimeout(()=>{
							plus.runtime.openFile(fileSaveUrl); //选择软件打开文件
						},1500)
					} else {
						//下载失败
						uni.hideLoading();
						plus.downloader.clear(); //清除下载任务
						uni.showToast({
							icon:'none',
							mask:true, 
							title: '下载失败，请稍后重试',
						});
					}
				})
				dtask.start();
				// uni.downloadFile({
				// 	url: url,//下载地址接口返回
				// 	success: (data) => {
				// 		if (data.statusCode === 200) {
				// 			//文件保存到本地
				// 			uni.saveFile({
				// 				tempFilePath: data.tempFilePath, //临时路径
				// 				success: function(res) {
				// 					uni.showToast({
				// 						icon: 'none',
				// 						mask: true,
				// 						title: '文件已保存：' + res.savedFilePath, //保存路径
				// 						duration: 3000,
				// 					});
				// 				}
				// 			});
				// 		}
				// 	},
				// 	fail: (err) => {
				// 		uni.showToast({
				// 			icon: 'none',
				// 			mask: true,
				// 			title: '失败请重新下载',
				// 		});
				// 	},
				// });
			},
			fileNameEscape(filename) {
				if (uni.getSystemInfoSync().platform == "ios") {
					filename = escape(filename);
				}
				return filename;
			},
			// 小程序保存文件
			WXSave(url){
				uni.downloadFile({
					url: url,//下载地址接口返回
					success: (data) => {
						if (data.statusCode === 200) {
							//文件保存到本地
							const FileSystemManager = wx.getFileSystemManager();
							FileSystemManager.saveFile({ //下载成功后保存到本地
								tempFilePath: data.tempFilePath,
								filePath: wx.env.USER_DATA_PATH + "/简历",
								success(res2) {
									if (res2.errMsg == 'saveFile:ok') {
										// 判断手机平台是 Android 还是 IOS
										if (uni.getSystemInfoSync().platform === 'android') {
											uni.showModal({
												title: "保存地址为",
												content: "手机存储/Android/data/com.tencent.mm/MicroMsg/wxanewfiles"
											})
										} else {
											uni.showToast({
												title: "请转移APP下载",
												icon: "none"
											})
										}
									} else {
										uni.showToast({
											title: "下载失败",
											icon: "none"
										})
									}
								},
								fail() {
									uni.showToast({
										title: "下载失败",
										icon: "none"
									})
								}
							})
						}
					},
					fail: (err) => {
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '失败请重新下载',
						});
					},
				});
			},
			jinzhi() {
				let _this = this;
				let param = window.location.href.split("#")[0];
				uni.request({
					method: "post",
					data: {
						url: param
					},
					header: {
						"content-type": "application/x-www-form-urlencoded",
					},
					url: "https://www.bangbangtongcheng.com/conven//mob/login/getJsTicket.do",
					success(re) {
						_this.jinzhifx(re.data);
					},
				});
			},
			jinzhifx(data) {
				wxshare.config({
					debug: false, //是否打开调试
					appId: data.appId, // 公众号的唯一标识
					timestamp: data.timestamp, // 生成签名的时间戳
			 	nonceStr: data.nonce, // ，生成签名的随机串
					signature: data.signature, // 签名
					jsApiList: ["hideMenuItems"],
				});
				// 禁止分享
				wxshare.hideMenuItems({
					// 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
					menuList: [
						"menuItem:share:appMessage",
						"menuItem:share:timeline",
						"menuItem:share:timeline",
						"menuItem:share:qq",
						"menuItem:favorite",
						"menuItem:share:QZone",
						"menuItem:openWithSafari",
					],
				});
			},
		},
	};
</script>
<style lang="less" scoped>
	.box {
		width: 100%;
		min-height: 100vh;
		background-color: #fff;
	}

	.head {
		position: fixed;
		top: 0;
		left: 0;
		width: 750rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 15px;
		background-color: #64B6A8;
		z-index: 1000;

		.fanhui {
			width: 40rpx;
			height: 40rpx;
		}

		.centers {
			font-size: 36rpx;
			font-weight: 400;
			color: #FFFFFF;
		}

		.head_right {
			font-size: 30rpx;
			font-weight: 400;
			color: #FFFFFF;
		}
		
		.image{
			width: 50rpx;
			height: 50rpx;
		}
	}
	.center{
		.title{
			font-size: 28rpx;
			font-weight: 600;
			color: #000000;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 32rpx 0 54rpx 0;
		}
		.image{
			width: 550rpx;
			height: 800rpx;
			border-radius: 10rpx;
			box-shadow: 1px 4rpx 10rpx 1px rgba(0, 0, 0, 0.12);
		}
	}
	.footer{
		position: fixed;
		bottom: 0;
		left: 0;
		height: 125rpx;
		width: 100%;
		background-color: #fff;
		display: flex;
		align-items: flex-start;
		justify-content: space-evenly;
		.btn{
			width: 300rpx;
			height: 80rpx;
			background-color: #64B6A8;
			border-radius: 12rpx;
			font-size: 34rpx;
			font-weight: 400;
			color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>
